/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package drinkcounter.web.servlet;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import java.io.IOException;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Toni
 */
@Singleton
public class RemovePartyServlet extends HttpServlet{

    @Inject
    private Provider<EntityManager> emProvider;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String partyIdentifier = req.getParameter("id");
        deleteParty(partyIdentifier);
        resp.sendRedirect("/parties");
    }

    private void deleteParty(String partyIdentifier){
        EntityManager em = emProvider.get();
        try{
            Query q =em.createQuery("DELETE FROM Party p WHERE p.identifier = :identifier");
            q.setParameter("identifier", partyIdentifier);
            q.executeUpdate();
        }finally{
            em.close();
        }
    }
}
